package cn.gjculture.shopping.api.controller;

import cn.gjculture.shopping.api.entity.Banner;
import cn.gjculture.shopping.api.entity.PageInfo;
import cn.gjculture.shopping.api.entity.Return;
import cn.gjculture.shopping.api.service.BannerService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value ="/banner" )
public class BannerController {

    @Autowired
    BannerService bannerService;

    @ApiOperation(value = "新增广告", notes = "新增广告")
    @ApiImplicitParam(name = "art", value = "广告对象", required = true, dataType = "Object")
    @PostMapping
    public Return save(Banner banner) {
        Return ret = new Return();
        if(bannerService.insert(banner)>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @PutMapping
    public Return<Banner> update(Banner banner){
        Return<Banner> ret=new Return<>();
        if(bannerService.update(banner)>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @GetMapping
    public Return<List<Banner>> query(PageInfo pageInfo) {
        Return<List<Banner>> ret = new Return<>();
        List<Banner> banners = bannerService.query();
        ret.setData(banners);
        if(!CollectionUtils.isEmpty(banners)){
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @GetMapping(value = "/{id}")
    public Return<Banner> getById(@PathVariable(value = "id") String id) {
        Return<Banner> ret = new Return<>();
        Banner banner = bannerService.queryById(id);
        ret.setData(banner);
        if(banner!=null){
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @DeleteMapping(value = "/{id}")
    public Return<Banner> delete(@PathVariable(value = "id") String id) {
        Return<Banner> ret = new Return<>();
        if(bannerService.delete(id,ret)>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }

}
